// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import LanguageMng from "./LanguageMng";
let urlPrefix = "http"

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        topNode:{
            default:null,
            type:cc.Node
        },

        webNode:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.webNode.width = cc.winSize.width
        this.webNode.height = cc.winSize.height - this.topNode.height

    },

    start () {
        let webView = this.webNode.getComponent(cc.WebView)
        // 设置一个JS跳转的规则，cocosjs是前缀，即当跳转界面cocosjs://xxxx时，会进行回调，查看下面的OnJSCallback()函数
        // webView.setJavascriptInterfaceScheme("cocosjs");
        webView.setJavascriptInterfaceScheme(urlPrefix);

        // 设置JS回调
        webView.setOnJSCallback(this.OnJSCallback.bind(this));
    },

    OnJSCallback: function(sender, url) {
        cc.log("JS回调 = " + url);
        url = decodeURIComponent(url)
        let prefix = urlPrefix+"://"
        let content = url.substr(prefix.length)
        let arr = content.split("&")
        let array = {}
        for(let i=0;i<arr.length;i++){
            let tmpArr = arr[i].split("=")
            array[tmpArr[0]] = tmpArr[1]
        }
        cc.log("解析完后：",JSON.stringify(array))
        if(array["key"] === "payment_result"){
            let status = array["status"]
            let errmsg = array["errmsg"]
            if(status == 1){
                this.webNode.active = false
                let title = LanguageMng.getSentence(3)
                let btnStr = LanguageMng.getSentence(57)
                let contentStr = LanguageMng.getSentence(168)
                let cb = function () {
                    cc.gg.gameDataUtil.goToLobby(true)
                }.bind(this)
                cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
            }else {
                this.webNode.active = false
                let title = LanguageMng.getSentence(3)
                let btnStr = LanguageMng.getSentence(57)
                let contentStr = LanguageMng.getSentence(169)
                let cb = function () {
                    // cc.gg.baseUtil.setLandscape(1)
                    cc.gg.gameDataUtil.goToLobby(true)
                    // cc.gg.toastMgr.removeDialog()
                }.bind(this)
                cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
            }
        }else {
            cc.error("Unknown js callback:",array["key"])
        }
    },



    // update (dt) {},

    onWebCloseClicked:function (event) {
        cc.gg.gameDataUtil.goToLobby(true)
    }
});
